package com.sky.service.admin.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.admin.DishMapper;
import com.sky.mapper.admin.SetmealDishMapper;
import com.sky.mapper.admin.SetmealMapper;
import com.sky.pojo.Setmeal;
import com.sky.pojo.SetmealDish;
import com.sky.result.PageResult;
import com.sky.service.admin.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

/**
 * @Description SetmealServiceImpl
 * @Author lyh
 * @Date 2024-09-21
 */
@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishMapper dishMapper;
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void save(SetmealDTO dto) {
       //拷贝属性
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(dto,setmeal);
        setmealMapper.save(setmeal);
        //回显，获取id
        Long id = setmeal.getId();
        //保存套餐中的菜品
        List<SetmealDish> setmealDishes = dto.getSetmealDishes();
        if(setmealDishes!=null && setmealDishes.size()>0){
            setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(id));
        }
      setmealDishMapper.saveMore(setmealDishes);
    }

    /**
     * 分页查询
     * @param dto
     * @return
     */
    @Override
    public PageResult pageQuery(SetmealPageQueryDTO dto) {
        PageHelper.startPage(dto.getPage(),dto.getPageSize());
        Page<SetmealVO> page =setmealMapper.pageQuery(dto);
        return new PageResult(page.getTotal(),page.getResult());
    }
    /**
     * 根据id查询套餐
     * @param setmealId
     * @return
     */
    @Override
    public SetmealVO getById(Long setmealId) {
        return setmealMapper.getById(setmealId);
    }
    /**
     * 批量删除套餐
     * @param ids
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Long[] ids) {
        for (Long id : ids) {
           if(setmealMapper.getById(id).getStatus()== StatusConstant.ENABLE){
               //起售中的套餐不能删除
               throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
           }
        }
        setmealMapper.delete(ids);
        setmealDishMapper.deleteBySetmealIds(ids);
    }
    /**
     * 修改套餐
     * @param dto
     * @return
     */
    @Override
    public void update(SetmealDTO dto) {
        //拷贝属性
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(dto,setmeal);
        //修改套餐
        setmealMapper.update(setmeal);
        Long id = setmeal.getId();
        //先删除原套餐对应的菜品
        Long[] ids={id};
        setmealDishMapper.deleteBySetmealIds(ids);
        //添加修改后套餐的菜品
        List<SetmealDish> setmealDishes = dto.getSetmealDishes();
        if(setmealDishes!=null && setmealDishes.size()>0){
            setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(id));
            setmealDishMapper.saveMore(setmealDishes);
        }
    }
    /**
     * 禁用启用套餐
     * @param status
     * @param id
     * @return
     */
    @Override
    public void stopOrStart(Integer status, long id) {
        //起售套餐时，判断套餐内是否有停售菜品，有停售菜品提示"套餐内包含未启售菜品，无法启售"
        if(status==StatusConstant.ENABLE) {
            SetmealVO setmealVO = setmealMapper.getById(id);
            List<SetmealDish> setmealDishes = setmealVO.getSetmealDishes();
            if (setmealDishes != null && setmealDishes.size() > 0) {
                for (SetmealDish setmealDish : setmealDishes) {
                    DishVO dish = dishMapper.getById(setmealDish.getDishId());
                    if (dish.getStatus() == StatusConstant.DISABLE) {
                        throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                    }
                }
            }
        }
        setmealMapper.stopOrStart(status,id);
    }

    /**
     * 根据分类id查询套餐
     * @param categoryId
     * @return
     */
    @Override
    public List<Setmeal> list(Long categoryId) {
        return setmealMapper.list(categoryId);
    }

    /**
     * 根据套餐id查询包含的菜品**
     * @param setmealId
     * @return
     */
    @Override
    public List<DishItemVO> getBysetmealId(Long setmealId) {
        return setmealDishMapper.getBySetmealId(setmealId);
    }
}
